package com.jay.beiange.efccp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jay.beiange.efccp.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/25 0025.
 */

public class ProductListAdapter extends BaseAdapter {

    private List<Map<String, Object>> data;
    private LayoutInflater mInflater;
    private Context context;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options; // 配置图片加载及显示选项


    public ProductListAdapter(Context context, List<Map<String, Object>> data){
        this.context=context;
        this.data=data;
        this.mInflater=LayoutInflater.from(context);
    }


    /**
     * 组件集合，对应list.xml中的控件
     * @author Administrator
     */
    static  class ViewHolder{
        public ImageView image;
        public TextView title;
        public TextView description;
    }
    @Override
    public int getCount() {
        return data.size();
    }
    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null){
            holder=new ViewHolder();
            //获得组件，实例化组件
            convertView=mInflater.inflate(R.layout.activity_product_list_item, null);
            holder.image=(ImageView)convertView.findViewById(R.id.id_product_item_image);
            holder.title=(TextView) convertView.findViewById(R.id.id_product_item_title);
            holder.description = (TextView) convertView.findViewById(R.id.id_product_item_desc);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }

        Glide.with(context)
                .load((String) data.get(position).get("image"))
                .centerCrop()
                .into(holder.image);
        holder.title.setText((String) data.get(position).get("title"));
        holder.description.setText((String) data.get(position).get("description"));
        return convertView;
    }

}
